package com.client.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * Created by Administrator on 2016/12/18.
 */
public class SendServer {
	/**
	 * 发送信息
	 */
	public void send(SendMsg msg) {
		try {
			//字节数组输出流
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(msg);

			byte [] b = bos.toByteArray();//将信息转换为字节数组

			InetAddress addr = InetAddress.getByName(msg.friendUserInfo.getIpAddr());//得到接收者的ip地址
			int port = msg.friendUserInfo.getPort();
			DatagramSocket socket = new DatagramSocket();
			DatagramPacket packet = new DatagramPacket(b , b.length , addr , port);

			//发送
			socket.send(packet);
			socket.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
